package com.udf.hive.udf.string;

import java.util.logging.Logger;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import jodd.util.StringUtil;

/**
 * 中文转数字
 * 
 * @作者 Administrator
 * @version [版本号, 2019年12月12日]
 */
public class ChineseToNumberUDF extends UDF
{

    private static Logger logger = Logger.getLogger("ChineseToNumberUDF");

    /**
     * 数据处理逻辑
     * 
     * @param str
     * @return
     */
    public Text evaluate(Text str) {
        if (str == null) {
            return null;
        }
        String value = String.valueOf(str).trim();
        int result;
        // 目前只对整型做转换
        try {
            result = getIntegerByNumberStr(value);
        }
        catch (Exception e) {
            result = 0;
            throw new RuntimeException(e);
        }

        return new Text(result + "");
    }

    public static void main(String[] args) {
        logger.info(String.valueOf(new ChineseToNumberUDF().evaluate(new Text("一十二亿三千四百五十六万七千八百九十"))));
    }

    /**
     * 支持到12位
     *
     * @param numberStr
     *            中文数字
     * @return int 数字
     */
    public int getIntegerByNumberStr(String numberStr) {

        // 返回结果
        int sum = 0;

        // null或空串直接返回
        if (numberStr == null || ("").equals(numberStr)) {
            return sum;
        }

        // 过亿的数字处理
        if (numberStr.indexOf('亿') > 0) {
            String currentNumberStr = numberStr.substring(0, numberStr.indexOf('亿'));
            int currentNumber = getPart(currentNumberStr);
            sum += currentNumber * Math.pow(10, 8);
            numberStr = numberStr.substring(numberStr.indexOf('亿') + 1);
        }

        // 过万的数字处理
        if (numberStr.indexOf('万') > 0) {
            String currentNumberStr = numberStr.substring(0, numberStr.indexOf('万'));
            int currentNumber = getPart(currentNumberStr);
            sum += currentNumber * Math.pow(10, 4);
            numberStr = numberStr.substring(numberStr.indexOf('万') + 1);
        }

        // 小于万的数字处理
        if (!("").equals(numberStr)) {
            int currentNumber = getPart(numberStr);
            sum += currentNumber;
        }

        return sum;
    }

    /**
     * 把亿、万分开每4位一个单元，解析并获取到数据
     * 
     * @param testNumber
     * @return
     */
    public int getPart(String testNumber) {
        // 返回结果
        int sum = 0;

        // null或空串直接返回
        if (testNumber == null || ("").equals(testNumber)) {
            return sum;
        }

        // 获取到千位数
        if (testNumber.indexOf('千') > 0) {
            String currentNumberStr = testNumber.substring(0, testNumber.indexOf('千'));
            sum += getBit(currentNumberStr) * Math.pow(10, 3);
            testNumber = testNumber.substring(testNumber.indexOf('千') + 1);
        }

        // 获取到百位数
        if (testNumber.indexOf('百') > 0) {
            String currentNumberStr = testNumber.substring(0, testNumber.indexOf('百'));
            sum += getBit(currentNumberStr) * Math.pow(10, 2);
            testNumber = testNumber.substring(testNumber.indexOf('百') + 1);
        }

        // 对于特殊情况处理 比如10-19是个数字，十五转化为一十五，然后再进行处理
        if (testNumber.indexOf('十') == 0) {
            testNumber = "一" + testNumber;
        }

        // 获取到十位数
        if (testNumber.indexOf('十') > 0) {
            String currentNumberStr = testNumber.substring(0, testNumber.indexOf('十'));
            sum += getBit(currentNumberStr) * Math.pow(10, 1);
            testNumber = testNumber.substring(testNumber.indexOf('十') + 1);
        }

        // 获取到个位数
        if (!("").equals(testNumber)) {
            sum += getBit(testNumber.replaceAll("零", ""));
        }

        return sum;
    }

    public int getBit(String number) {
        if (StringUtil.isNotBlank(number)) {
            switch (number) {
                case "一":
                    return 1;
                case "二":
                    return 2;
                case "三":
                    return 3;
                case "四":
                    return 4;
                case "五":
                    return 5;
                case "六":
                    return 6;
                case "七":
                    return 7;
                case "八":
                    return 8;
                case "九":
                    return 9;
                case "零":
                    return 0;
                default:
                    return 0;
            }
        }
        else {
            return 0;
        }
    }
}
